package es.goldbittle.android;

//import android.R;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import es.goldbittle.android.R;

 
import java.util.ArrayList;

public class foroAdapter extends BaseExpandableListAdapter {
 
	private int[] colors = new int[] { 0xFF282828, 0xFF505050 };
	private int[] colorsChild = new int[] { 0xFFAAAAAA, 0xFFBBBBBB };
    private LayoutInflater inflater;
    private ArrayList<parentForo> mParent;
 
    public foroAdapter(Context context, ArrayList<parentForo> parent){
        mParent = parent;
        inflater = LayoutInflater.from(context);

    }
 
 
   
    //counts the number of group/parent items so the list knows how many times calls getGroupView() method
    public int getGroupCount() {
        return mParent.size();
    }
 

    //counts the number of children items so the list knows how many times calls getChildView() method
    public int getChildrenCount(int i) {
        return mParent.get(i).getArrayChildren().size();
    }
 

    //gets the title of each parent/group
    public Object getGroup(int i) {
        return mParent.get(i).getTitle();
    }
    public Object getGroupTitulo(int i) {
        return mParent.get(i).getTitle();
    }
    public Object getGroupMensaje(int i) {
        return mParent.get(i).getMensaje();
    }
    public Object getGroupFecha(int i) {
        return mParent.get(i).getFecha();
    }
 
    //gets the name of each item
    public Object getChild(int i, int i1) {
        return mParent.get(i).getArrayChildren().get(i1);
    }
 
    public long getGroupId(int i) {
        return i;
    }

    public long getChildId(int i, int i1) {
        return i1;
    }
 
   
    public boolean hasStableIds() {
        return true;
    }
 

    //in this method you must set the text to see the parent/group on the list
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
 
        if (view == null) {
            view = inflater.inflate(R.layout.list_item_parent_foro, viewGroup,false);
        }
 
        //TextView textView = (TextView) view.findViewById(R.id.list_item_text_view);
        TextView textViewTitulo = (TextView) view.findViewById(R.id.list_item_parent_foro_tiulo);
        TextView textViewMensaje = (TextView) view.findViewById(R.id.list_item_parent_foro_mensaje);
        TextView textViewFecha = (TextView) view.findViewById(R.id.list_item_parent_foro_fecha);
        //"i" is the position of the parent/group in the list
        textViewTitulo.setText(getGroupTitulo(i).toString());
        textViewMensaje.setText(getGroupMensaje(i).toString());
        textViewFecha.setText(getGroupFecha(i).toString());
 
	    //Set the background color depending of  odd/even colorPos result
	    int colorPos = i % colors.length;
	    view.setBackgroundColor(colors[colorPos]);
        
        //return the entire view
        return view;
    }
 
    //in this method you must set the text to see the children on the list
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.list_item_child_foro, viewGroup,false);
        	//view = inflater.inflate(R.layout.list_item_child_foro, root);
        }
        
 
        TextView textViewUsuario = (TextView) view.findViewById(R.id.list_item_text_child_usuario);
        TextView textViewFecha = (TextView) view.findViewById(R.id.list_item_text_child_fecha);
        TextView textViewComentario = (TextView) view.findViewById(R.id.list_item_text_child_comentario);
        //"i" is the position of the parent/group in the list and 
        //"i1" is the position of the child
        textViewUsuario.setText(mParent.get(i).getArrayChildren().get(i1).getUsuario());
        textViewFecha.setText(mParent.get(i).getArrayChildren().get(i1).getFecha());
        textViewComentario.setText(mParent.get(i).getArrayChildren().get(i1).getComentario());
 
	    //Set the background color depending of  odd/even colorPos result
	    int colorPos = i1 % colorsChild.length;
	    view.setBackgroundColor(colorsChild[colorPos]);

        //return the entire view
        return view;
    }
 
    
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
 

    public void registerDataSetObserver(DataSetObserver observer) {
        /* used to make the notifyDataSetChanged() method work */
        super.registerDataSetObserver(observer);
    }
}